<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>student</title>
    <link rel="stylesheet" href="/bootstrap.min.css">
    <link rel="stylesheet" href="/bootstrap-table.min.css">

    <style>
        #l1, #name1, #l2, #no1, #bt {
            display: inline-block;
            width: auto;
            margin-right: 20px;
        }

        #search {
            margin: 40px 0;
        }

        a {
            text-decoration: none;
            color: #ea2d62;
        }

    </style>
    <script src="/jquery.min.js"></script>

</head>
<body>
<div class="container">
    <form class="form-inline" id="search">
        <label class="sr-only" for="name1" id="l1">请输入姓名</label>
        <input type="text" class="form-control" id="name1" name="name" placeholder="name">
        <label class="sr-only" for="no1" id="l2">请输入学号</label>
        <input type="text" class="form-control" id="no1" name="sno" placeholder="sno">
        <button type="button" class="btn btn-primary" id="bt" onclick="findByNameOrSno();">搜索</button>
    </form>
    <!--模态框-->
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal"
            onclick="preAdd()">
        新增学生信息
    </button>
    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h1 class="modal-title fs-5" id="exampleModalLabel">学生信息</h1>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="StudentInfor">
                        <input hidden="hidden" id="id" name="id"/>
                        <div class="form-group">
                            <label for="sno">学号</label>
                            <input type="text" class="form-control" id="sno" name="sno" placeholder="sno">
                        </div>
                        <div class="form-group">
                            <label for="name">姓名</label>
                            <input type="text" class="form-control" id="name" name="name" placeholder="name">
                        </div>
                        <div class="form-group">
                            <label for="sex">性别：</label>
                            <select class="form-control" id="sex" name="sex">
                                <option value="1">男</option>
                                <option value="2">女</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="score">成绩</label>
                            <input type="text" class="form-control" id="score" name="score" placeholder="score">
                        </div>
                        <div class="form-group">
                            <label for="age">年龄</label>
                            <input type="text" class="form-control" id="age" name="age" placeholder="age">
                        </div>

                        <div class="form-group">
                            <label for="password">密码</label>
                            <input type="password" class="form-control" id="password" name="password"
                                   placeholder="password">
                        </div>

                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" onclick="saveStudent();" data-dismiss="modal">保存
                    </button>

                </div>
            </div>
        </div>
    </div>

    <table id="stuTable" class="table table-bordered table-hover table-condensed" style="text-align: center;">


    </table>
</div>


<!--发送请求返回数据-->
<script src="/bootstrap.min.js"></script>
<script src="/bootstrap-table.min.js"></script>
<script src="/locale/bootstrap-table-zh-CN.min.js"></script>
<script>
    function preAdd() {
        //清空模态框数据
        $("#name").val("")
        $("#sno").val("")
        $("#score").val("")
        $("#age").val("")
        $("#password").val("")

        $("#id").val(0)

    }

    // 根据姓名模糊查询或者根据学号查询
    function findByNameOrSno() {
        $("#stuTable").bootstrapTable("destroy")
        getStudentList()
        $("#name1").val(" ")
        $("#no1").val(" ")
    }

    // 获取学生全部信息
    function getStudentList() {
        $("#stuTable").bootstrapTable({
            url: "/webapi/student/getbypage",  //请求后台的URL（*）
            striped: true,   //是否显示行间隔色
            pagination: true,  //是否显示分页（*）
            singleSelect: false,  //是否可以多选
            pageSize: 5,  //每页的记录行数（*）
            pageNumber: 1,  //初始化加载第一页，默认第一页,并记录
            sidePagination: "server",  //分页方式：client客户端分页，server服务端分页（*）

            //得到查询的参数
            queryParams: function (params) {
                //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                var paraObj = {
                    size: params.limit,
                    page: params.offset / params.limit,
                    sort: "id",
                    direct: "desc",
                    name: $("#name1").val().trim(),
                    no: $("#no1").val().trim(),
                }
                return paraObj
            },

            columns: [{
                field: "id",
                title: "序号"
            }, {
                field: "name",
                title: "姓名"
            }, {
                field: "sno",
                title: "学号"
            }, {
                field: "score",
                title: "成绩"
            }, {
                field: "age",
                title: "年龄"
            }, {
                field: "sex",
                title: "性别"
            }, {
                field: "handle",
                title: "操作",
                formatter: (value, row, index) => {
                    return "<a href='#' onclick='editStudent(" + row.id + ");'>编辑</a> <a href='#' onclick='deleteStudent(" + row.id + ");'>删除</a>"
                }
            }]

        })
    }

    // 新增或者更新
    function saveStudent() {
        var data = $("#StudentInfor").serialize()
        var id = $("#id").val()
        // 新增
        if (id < 1) {
            $.ajax({
                url: "/webapi/student/insert",
                method: "post",
                data: data
            }).done(function () {
                $("#stuTable").bootstrapTable("destroy")
                getStudentList()
                $("#exampleModal").modal("hide");
            })
        } else {
            // 更新
            $.ajax({
                url: "/webapi/student/update",
                method: "put",
                data: data
            }).done(function () {
                $("#stuTable").bootstrapTable("destroy")
                getStudentList()
                $("#exampleModal").modal("hide");
            })
        }


    }

    // 编辑学生信息
    function editStudent(id) {
        $("#exampleModal").modal("show");
        $.ajax({
            url: "/webapi/student/get/" + id
        }).done(function (res) {
            $("#id").val(res.id)
            $("#name").val(res.name)
            $("#sno").val(res.sno)
            $("#sex").val(res.sex)
            $("#score").val(res.score)
            $("#age").val(res.age)
            $("#password").val("")
        })
    }

    // 删除学生信息
    function deleteStudent(id) {
        if (confirm("你确定要删除吗？")) {
            $.ajax({
                url: "/webapi/student/delete/" + id,
                method: "delete"
            }).done(function () {
                $("#stuTable").bootstrapTable("destroy")
                getStudentList()
            })

        }
    }

    $(function () {
        getStudentList()
    })

</script>

</body>

</html>